package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.informer.service.LoginService;
import com.metagurukul.informer.utility.ExceptionProperties;
import com.metagurukul.informer.utility.Validator;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * this function validate that if this user is registered or not if
	 * registered than logged in else forwarded msg that you are not registered
	 * yet
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Validator validator = new Validator();
		Map<String, String> exceptiomap = new HashMap<String, String>();

		String username = request.getParameter("emailsignup");
		String password = request.getParameter("password");

		if (username == null) {
			exceptiomap.put("emailsignup", ExceptionProperties._101);
		} else {
			if (username == "") {
				exceptiomap.put("emailsignup", ExceptionProperties._106);
			} else {
				if (!validator.emailValidate(username)) {
					// System.out.println("emailvalidator  :"+ep._102);
					exceptiomap.put("emailsignup", ExceptionProperties._102);
				}
			}
		}
		if (password == null) {
			exceptiomap.put("password", ExceptionProperties._101);
		} else {
			if (password == "") {
				exceptiomap.put("password", ExceptionProperties._106);
			}
		}
		// if there is no exception
		if (exceptiomap.size() == 0) {

			LoginService loginservice = new LoginService(username, password);
			try {
				// if user is Registered
				if (loginservice.isUserExist()) {
					String result[] = loginservice.doLogin();

					if (result[0] != null) {
						HttpSession sessionName = request.getSession();
						sessionName.setAttribute("name", result[1]);
						if (result[0].equals("user")) {
							sessionName.setAttribute("userName", username);
						} else {
							sessionName.setAttribute("adminName", username);
						}
						response.sendRedirect("ShowQuestions");

					} else {
						exceptiomap.put("loginmsg", ExceptionProperties._109);
						request.setAttribute("exceptionmap", exceptiomap);
						request.getRequestDispatcher("/login.jsp").forward(
								request, response);
					}
				} else {
					//if user is registered but logging in first time 
					if (loginservice.isTempUser()) {
						loginservice.finalRegisterUser();
						String result[] = loginservice.doLogin();

						if (result[0] != null) {
							HttpSession sessionName = request.getSession();
							sessionName.setAttribute("name", result[1]);
							if (result[0].equals("user")) {
								sessionName.setAttribute("userName", username);
							} else {
								sessionName.setAttribute("adminName", username);
							}
							response.sendRedirect("ShowQuestions");
							return;
						} else {
							exceptiomap.put("loginmsg",
									ExceptionProperties._109);
							request.setAttribute("exceptionmap", exceptiomap);
							request.getRequestDispatcher("/login.jsp").forward(
									request, response);
						}
						response.sendRedirect("ShowQuestions");
						return;
					} else {
						exceptiomap.put("loginmsg", ExceptionProperties._110);
						request.setAttribute("exceptionmap", exceptiomap);
						request.getRequestDispatcher("/login.jsp").forward(
								request, response);
					}

				}
			} catch (Exception e) {
				response.sendRedirect("Error.jsp");
				return;
			}

		} else {

			request.setAttribute("exceptionmap", exceptiomap);
			request.getRequestDispatcher("/login.jsp").forward(request,
					response);
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

}
